//
//  MineViewController.m
//  HeartTravel
//
//  Created by dlios on 15-7-8.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MineViewController.h"
#import "SDImageCache.h"
#import "UIImageView+WebCache.h"
#import "SelfImageViewController.h"


#define ImageHight 200.0f
#define WIDTH self.view.frame.size.width

@interface MineViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)UITableView *mineTableView;

@property(nonatomic, retain)UIImageView *zoomImageView;

@property(nonatomic, assign)CGFloat myalpha;

@property(nonatomic,retain)UIVisualEffectView *visualEffectView;

@property(nonatomic,retain)UIImageView *circleView;

@property(nonatomic,retain)UILabel *textLabel;

@property(nonatomic,retain)NSString *cacheSize;

@end

@implementation MineViewController

- (void)dealloc{
    [_circleView release];
    [_textLabel release];
    [_visualEffectView release];
    [_mineTableView release];
    [_zoomImageView release];
    [super dealloc];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //隐藏：NavigationBar
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    [self createMineTableView];
    [self createZoomImage];
    [self createVisualEffect];
    [self createCircleView];
    

//    
    NSString *path=[self cachesPath];
    CGFloat size=[self folderSizeAtPath:path];
    NSLog(@"will--%f",size);
    
    self.cacheSize=[NSString stringWithFormat:@"%0.1fM",size];
    NSLog(@"will---%@",self.cacheSize);
    
    
    
    [_visualEffectView release];
    [_mineTableView release];
    [_zoomImageView release];
    [_textLabel release];
    
    
}

/**
 *  创建tableView
 */
- (void)createMineTableView{
    
    self.mineTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    
    //设置代理
    self.mineTableView.delegate = self;
    self.mineTableView.dataSource = self;
    
    //设置偏移量属性
    self.mineTableView.contentInset = UIEdgeInsetsMake(ImageHight, 0, 0, 0);
    
    [self.view addSubview:self.mineTableView];
    

}

/**
 *  创建头部视图
 */
- (void)createZoomImage{
    
    self.zoomImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.jpg"]];
    self.zoomImageView.frame = CGRectMake(0, -ImageHight, WIDTH, ImageHight);
    [self.mineTableView addSubview:self.zoomImageView];
    
}

/**
 *  创建毛玻璃
 */
- (void)createVisualEffect{
    
    self.myalpha = 0.9;
    self.visualEffectView = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    self.visualEffectView.frame = self.zoomImageView.frame;
    self.visualEffectView.alpha = self.myalpha;
    self.visualEffectView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
    [self.mineTableView addSubview:self.visualEffectView];
    
}

/**
 *  设置头像
 */
- (void)createCircleView{
    
    //设置autoresizesSubviews让子类自动布局
    self.zoomImageView.autoresizesSubviews=YES;
    self.circleView = [[UIImageView alloc]initWithFrame:CGRectMake(10, ImageHight - 50, 40, 40)];
    self.circleView.backgroundColor = [UIColor whiteColor];
    self.circleView.layer.cornerRadius = 7.5;
    //头像图片
    self.circleView.image = [UIImage imageNamed:@"iconfont-favorfill"];
    //如果子视图的范围创出了父视图的边界,那么超出的部分就会被剪裁掉.
    self.circleView.clipsToBounds = YES;
    //自适应底部
    //    self.circleView.autoresizingMask=UIViewAutoresizingFlexibleBottomMargin;
    //自动布局，自适应顶部
    self.circleView.autoresizingMask=UIViewAutoresizingFlexibleTopMargin;
    [self.visualEffectView addSubview:self.circleView];
    
    //设置标题
    self.textLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, ImageHight-40, 280, 20)];
    self.textLabel.textColor = [UIColor whiteColor];
    self.textLabel.text = @"心途";
    //自动布局，自适应顶部
    self.textLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [self.visualEffectView addSubview:self.textLabel];
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat y = scrollView.contentOffset.y;
    
    if (y < -ImageHight) {
//        CGRect frame = self.zoomImageView.frame;
//        frame.origin.y = y;
//        frame.size.height = -y;
        
        self.visualEffectView.alpha = (320+y-10)/100;
        self.visualEffectView.frame = CGRectMake(0, y, WIDTH/200*(-y), -y);
        self.zoomImageView.frame = CGRectMake(0, y, WIDTH/200*(-y), -y);
        self.zoomImageView.center = CGPointMake(WIDTH/2, y/2);
    }
    if (y < -300) {
        SelfImageViewController *selfVC = [[SelfImageViewController alloc]init];
        [self.navigationController pushViewController:selfVC animated:YES];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 2) {
        return 2;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"reuse"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.separatorInset=UIEdgeInsetsZero;
        cell.clipsToBounds=YES;
    }
    
    switch (section) {
        case 0:
            cell.textLabel.text= @"收藏";
            break;
        case 1:
            cell.textLabel.text=@"清除缓存";
            cell.detailTextLabel.text=self.cacheSize;
            break;
        case 2:
            if(row == 0){
                cell.textLabel.text=@"关于我们";
            }else{
                cell.textLabel.text=@"意见反馈";
            }
            break;
            
        default:
            break;
    }
    
    //    cell.textLabel.text=[NSString stringWithFormat:@"第%ld行",(long)indexPath.row];
    return cell;
    
}

//选择cell时方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row=indexPath.row;
    NSInteger section=indexPath.section;
    
    switch (section) {
        case 0:
            NSLog(@"shou");
            break;
        case 1:
            //            NSLog(@"qingchu");
            //            [self clearCache];
            [self clearAlert];
            break;
        case 2:
            if (row == 0) {
                NSLog(@"about");
            }else{
                NSLog(@"意见反馈");
            }
            
            break;
        default:
            break;
    }
}

//清除缓存alert
-(void)clearAlert{
    if ([self.cacheSize isEqualToString:@"0M"]) {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"没有需要清除的缓存哦" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }else{
        NSString *alertStr=[NSString stringWithFormat:@"要清除%@的缓存么?",self.cacheSize];
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"清除缓存" message:alertStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//        [self clearCache];
        [alert show];
        [alert release];
    }
}


//清除缓存
-(void)clearCache{
    
    
    //创建一个文件管理者
    NSFileManager *manager=[NSFileManager defaultManager];
    
    
    //获取缓存文件路径
    NSString *path=[self cachesPath];
    //删除缓存文件
    [manager removeItemAtPath:path error:nil];
    CGFloat sizef=[self folderSizeAtPath:path];
    NSLog(@"%f",sizef);
    
    self.cacheSize=[NSString stringWithFormat:@"%ldM",(long)sizef];
    [self.mineTableView reloadData];
}

//缓存文件路径
-(NSString *)cachesPath{
    //创建一个文件管理者
    //    NSFileManager *manager=[NSFileManager defaultManager];
    NSArray *cachesArr=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, 1, YES);
    NSString *cachesPath=cachesArr[0];
    //    NSLog(@"%@",cachesPath);
    return cachesPath;
}
//计算单个文件大小
-(float)fileSizeAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]){
        long long size=[fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size/1024.0/1024.0;
        
    }
    return 0;
}

//计算目录大小
-(float)folderSizeAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    float folderSize;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            folderSize +=[self fileSizeAtPath:absolutePath];
        }
        //SDWebImage框架自身计算缓存的实现
        folderSize+=[[SDImageCache sharedImageCache] getSize]/1024.0/1024.0;
        return folderSize;
    }
    return 0;
}

-(void)viewWillAppear:(BOOL)animated{
    NSString *path=[self cachesPath];
    CGFloat size=[self folderSizeAtPath:path];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    NSLog(@"WillAppear--%f",size);
    
    self.cacheSize=[NSString stringWithFormat:@"%0.1fM",size];
    NSLog(@"WillAppear---%@",self.cacheSize);
    [self.mineTableView reloadData];
}

//根据被点击按钮的索引处理点击事件
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 1){
//        NSLog(@"clickButtonAtIndex:%ld",(long)buttonIndex);
        [self clearCache];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
